package com.whl.sm.controller.expense;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.controller.BaseController;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.expense.ContractTenancy;
import com.whl.sm.model.expense.TenancyBill;
import com.whl.sm.model.inhabitant.AssetsTenant;
import com.whl.sm.model.system.OrgUserInfo;
import com.whl.sm.service.assets.ComUnitsService;
import com.whl.sm.service.expense.ContractTenancyService;
import com.whl.sm.service.expense.TenancyBillService;
import com.whl.sm.service.inhabitant.AssetsTenantService;
import com.whl.sm.service.system.OrgUserInfoService;
import com.whl.sm.service.system.OrgUserService;
import com.whl.sm.vo.TenancyBillVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-25
 */
@RestController
@RequestMapping("/tenancy-bill")
public class TenancyBillController extends BaseController {

    @Autowired
    TenancyBillService tenancyBillService;

    @Autowired
    ContractTenancyService contractTenancyService;

    @Autowired
    ComUnitsService comUnitsService;

    @Autowired
    AssetsTenantService assetsTenantService;

    @Autowired
    OrgUserService orgUserService;

    @Autowired
    OrgUserInfoService orgUserInfoService;

    @RequestMapping("/list")
    public Result getTenBillList(HttpServletRequest request){
        QueryWrapper queryWrapper = new QueryWrapper();
        setHttpRequestQueryWrapper(queryWrapper,"unit_id","payment_status");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> billPage = tenancyBillService.page(getPage(),queryWrapper);
        List<Object> resultPage = billPage.getRecords().stream().map(obj->{
            TenancyBill tenancyBill = (TenancyBill) obj;
            ComUnits comUnits = comUnitsService.getById(tenancyBill.getUnitId());
            OrgUserInfo userInfo = null;
            if (tenancyBill.getPayer() != null){
                userInfo = orgUserInfoService.getOne(new QueryWrapper<OrgUserInfo>()
                        .eq("user_id",tenancyBill.getPayer())
                );
            }
            return new TenancyBillVo(tenancyBill,comUnits,userInfo);
        }).collect(Collectors.toList());
        billPage.setRecords(resultPage);
        return Result.ok(billPage);
    }


    @RequestMapping("/getByCurrent")
    public Result getByCurrent(){
        String userId = orgUserService.getCurrentUserId();
        List<AssetsTenant> tenantList = assetsTenantService.list(new QueryWrapper<AssetsTenant>()
                .eq("user_id",userId)
                .eq("visible",1)
                .ne("is_del",1)
        );
        if (tenantList.size() == 0){
            return Result.fail();
        }
        AssetsTenant linkTenant = tenantList.get(0);
        List<ContractTenancy> linkContracts = contractTenancyService.list(new QueryWrapper<ContractTenancy>()
                .eq("tenant_id",linkTenant.getId())
                .ne("is_del",1)
        );
        if (linkContracts.size() == 0){
            return Result.fail();
        }
        ContractTenancy linkContract = linkContracts.get(0);
        QueryWrapper queryWrapper = new QueryWrapper();
        setHttpRequestQueryWrapper(queryWrapper,"payment_status");
        queryWrapper.eq("contract_id",linkContract.getId());
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("update_time");
        Page<Object> billPage = tenancyBillService.page(getPage(),queryWrapper);
        List<Object> resultPage = billPage.getRecords().stream().map(obj->{
            TenancyBill tenancyBill = (TenancyBill) obj;
            ComUnits comUnits = comUnitsService.getById(tenancyBill.getUnitId());
            OrgUserInfo userInfo = null;
            if (tenancyBill.getPayer() != null){
                userInfo = orgUserInfoService.getOne(new QueryWrapper<OrgUserInfo>()
                        .eq("user_id",tenancyBill.getPayer())
                );
            }
            return new TenancyBillVo(tenancyBill,comUnits,userInfo);
        }).collect(Collectors.toList());
        billPage.setRecords(resultPage);
        return Result.ok(billPage);
    }

    @RequestMapping("/info/{id}")
    public Result tenBillInfo(@PathVariable("id") String id){
        TenancyBill tenancyBill = tenancyBillService.getById(id);
        return Result.ok(tenancyBill);
    }


    @RequestMapping("/update")
    public Result updateBill (@RequestBody TenancyBill tenancyBill){
        return tenancyBillService.update(tenancyBill);
    }

    @RequestMapping("/buildBill")
    public Result buildBill (@RequestBody TenancyBill tenancyBill){
        return tenancyBillService.addBill(tenancyBill);
    }

    @RequestMapping("/payBill")
    public Result payBill(@RequestParam("billId")String billId){
        return tenancyBillService.payBill(billId);

    }

    @RequestMapping("/sendMessage")
    public Result sendMessage(@RequestBody List<String> idList){
        return tenancyBillService.sendMessage(idList);
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            tenancyBillService.update(new UpdateWrapper<TenancyBill>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

}
